﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DataMining.Properties;
using System.Windows;
using System.Data;


namespace DataMining.DataAccess
{
    public class GetNum
    {
        /// <summary>
        /// 获取时间最大数
        /// </summary>
        /// <param name="attr">输入属性</param>
        /// <param name="tablename">输入表名</param>
        /// <returns></returns>
        public DateTime GetMaxNumber(string attr,string tablename)
        {
            
            DateTime MaxCTime;
            object maxtime;
            DataAccess.MyConnection myconn=new MyConnection();
            SqlConnection connection = myconn.getSqlConnection2();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandTimeout = 180;
            command.CommandText = "select max("+attr+") from "+tablename;
            maxtime = command.ExecuteScalar();

            if (maxtime.ToString()!="")
            {
                DateTime max_time = Convert.ToDateTime(maxtime);
                MaxCTime = max_time;
            }
            else
            {
                MaxCTime = Convert.ToDateTime("2009-05-04 00:00:00");
            }

            connection.Close();
            return MaxCTime;
            
        }

        /// <summary>
        /// 查询目标数据库某个表，某段时间的数据量
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="start_time">开始时间</param>
        /// <param name="end_time">结束时间</param>
        /// <returns></returns>
        public int GetCount(string tablename,DateTime start_time,DateTime end_time)
        {
            int count;
            DataAccess.MyConnection myconn = new MyConnection();
            SqlConnection connection = myconn.getSqlConnection2();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select count(CJSJ) from " + tablename+" where CJSJ between '"+start_time.ToString()+"' and '"+end_time.ToString()+"'";
            count = Convert.ToInt32(command.ExecuteScalar());
            connection.Close();
            return count;
        }

        /// <summary>
        /// 查询目标数据库中某个表大于某一时间的所有数据量
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="start_time">开始时间</param>
        /// <returns></returns>
        public int GetCount2(string tablename, DateTime start_time)
        {
            int count;
            MyConnection myconn = new MyConnection();
            SqlConnection connection = myconn.getSqlConnection2();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select count(CJSJ) from " + tablename + " where CJSJ >= '" + start_time.ToString() + "'" ;
            count = Convert.ToInt32(command.ExecuteScalar());
            connection.Close();
            return count;
        }

        /// <summary>
        /// 查询数据源某个表，某一时间段的数据量
        /// </summary>
        /// <param name="tablename">表明</param>
        /// <param name="start_time">开始时间</param>
        /// <param name="end_time">结束时间</param>
        /// <returns></returns>
        public int GetDBcount(string tablename, DateTime start_time, DateTime end_time)
        {
            int count;
            DBConnection dbconn = new DBConnection();
            SqlConnection connection = dbconn.getSqlConnection1();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select count(create_time) from " + tablename + " where create_time between '"+start_time.ToString()+"' and '"+end_time.ToString()+"'";
            count = Convert.ToInt32(command.ExecuteScalar());
            connection.Close();
            return count;
        }

    }
}
